﻿using Microsoft.Extensions.Options;
using MongoDB.Driver;
using SubStationManagement.Server.Models;

namespace SubStationManagement.Server.Services
{
    public class RecordingModuleService
    {
        private readonly IMongoCollection<RecordingModule> _RecordingModulesCollection;

        public RecordingModuleService(
            IOptions<SubStationDatabaseSettings> subStationDatabaseSettings)
        {
            var mongoClient = new MongoClient(
                subStationDatabaseSettings.Value.ConnectionString);

            var mongoDatabase = mongoClient.GetDatabase(
                subStationDatabaseSettings.Value.DatabaseName);

            _RecordingModulesCollection = mongoDatabase.GetCollection<RecordingModule>(
                subStationDatabaseSettings.Value.RecordingModulesCollectionName);
        }

        public async Task<List<RecordingModule>> GetAsync() =>
            await _RecordingModulesCollection.Find(_ => true).ToListAsync();

        public List<RecordingModule> GetAll() =>
            _RecordingModulesCollection.Find(_ => true).ToList();

        public async Task<RecordingModule?> GetAsync(string id) =>
            await _RecordingModulesCollection.Find(x => x.Id == id).FirstOrDefaultAsync();

        public async Task CreateAsync(RecordingModule newRecordingModule) =>
            await _RecordingModulesCollection.InsertOneAsync(newRecordingModule);

        public async Task UpdateAsync(string id, RecordingModule updatedRecordingModule) =>
            await _RecordingModulesCollection.ReplaceOneAsync(x => x.Id == id, updatedRecordingModule);

        public async Task RemoveAsync(string id) =>
            await _RecordingModulesCollection.DeleteOneAsync(x => x.Id == id);
    }
}
